package cn.com.hp.feature.onfig;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * @author hongjuzhu
 * @date 2022/3/9
 */
@Configuration
@MapperScan("com.marketplace.mpsubscribe.repository")
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static String tenantId = "HP";
    private static String USER_ID ="userId";

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "isDeleted", Boolean.class, false);
        this.strictInsertFill(metaObject, "tenantId", String.class, tenantId);
        //创建数据默认将请求头拦截下来的用户id填充到createBy与updateBy
        this.strictInsertFill(metaObject, "createUser", String.class, UserProperties.getCurrentUserId(USER_ID));
        this.strictInsertFill(metaObject, "updateUser", String.class, UserProperties.getCurrentUserId(USER_ID));
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //每次更新强制修改更新日期，只适用于updateById有效
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateUser", UserProperties.getCurrentUserId(USER_ID), metaObject);
    }
}
